package com.th.config.Interceptor;

import com.th.common.domain.UsSessionToken;
import com.th.common.exception.BaseServiceException;
import com.th.common.response.ExceptionResultData;
import com.th.service.UsSessionTokenService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.AsyncHandlerInterceptor;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.Objects;

/**
 * 认证处理拦截器
 * @Author tuhang
 * @PackageName nswcy-crowdsource-client-server
 * @Package com.th.config.Interceptor
 * @Date 2023/6/7 12:15
 * @Version 1.0
 */
@Component
@Slf4j
public class AuthHandlerInterceptor implements HandlerInterceptor {


    @Resource
    private UsSessionTokenService usSessionTokenService;

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        log.info("执行了认证处理拦截器的preHandle方法");

        String token = request.getHeader("token");
       if (StringUtils.isBlank(token)){
           throw new BaseServiceException(ExceptionResultData.NO_LOGIN);
       }else {
           //TODO 查询当前令牌是否过期 
           UsSessionToken usSessionToken = usSessionTokenService.getToken(token);
           if (Objects.isNull(usSessionToken)) {
               throw new BaseServiceException(ExceptionResultData.NO_LOGIN);
           }
           Date createTime = usSessionToken.getCreateTime();
           Date expireTime = usSessionToken.getExpireTime();
           Date now = new Date();

           //如果令牌已经过期
           if((now.getTime() > expireTime.getTime())){
               usSessionTokenService.removeById(usSessionToken.getId());
               log.info("令牌已过期");
               throw new BaseServiceException(ExceptionResultData.CURRENT_USER_SESSION_IS_EXPIRE);
           }
           /**
            * 如果过期时间与创建时间小于5分钟则续期
            * 过期时间 16:00
            * 当前时间 16:50
            */
           if((expireTime.getTime() - now.getTime()) <= 5*60*1000){
               Boolean aBoolean = usSessionTokenService.renewToken(usSessionToken);
               if (aBoolean){
                   log.info("当前会话续期成功！！！{}",token);
                   return true;
               }else {
                   throw new BaseServiceException(ExceptionResultData.CURRENT_USER_SESSION_IS_EXPIRE);
               }
           }
       }

        return true;
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
        log.info("执行了拦截器的postHandle方法");
    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
        log.info("执行了拦截器的afterCompletion方法");
    }
}

